<!-- 五星好评组件 -->
<template>
  <div class="five_stars">
    <span
      v-for="(item, index) in 5"
      :key="index"
      :style="{
        fontSize: size + `px`,
      }"
      :class="[`iconfont`, 'ui-Star', index < realNum ? `active` : ``]"
      @click="onItemClick(index)"
    >
    </span>
  </div>
</template>

<script>
export default {
  name: "stars",
};
</script>

<script setup>
import { useStars } from "../../hooks";
//定义接受的数据
const props = defineProps({
  //大小
  size: {
    type: Number,
    default: 25,
  },
  //传递的星级等级
  num: {
    type: Number,
    default: 0,
  },
  //是否允许进行操作,默认只显示不允许操作
  isArrowClick: {
    type: Boolean,
    default: false,
  },
});

//定义触发的事件
const emits = defineEmits(["getStarNums"]);

//使用hooks来定义操作组件的方法
const [realNum, setStars] = useStars(props.num, () => {
  emits("getStarNums", realNum.value);
});

//条目点击事件
const onItemClick = (index) => {
  if (props.isArrowClick) {
    //是允许操作的
    setStars(index);
  }
};
</script>

<style lang="scss" scoped>
@font-face {
  font-family: "iconfont";
  src: url("//at.alicdn.com/t/c/font_4545737_bhvd0a37md.woff2?t=1715667880818")
      format("woff2"),
    url("//at.alicdn.com/t/c/font_4545737_bhvd0a37md.woff?t=1715667880818")
      format("woff"),
    url("//at.alicdn.com/t/c/font_4545737_bhvd0a37md.ttf?t=1715667880818")
      format("truetype");
}

.iconfont {
  font-family: "iconfont" !important;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #999;
  &.ui-Star {
    transition: color 0.3s linear;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
  &.ui-Star:before {
    content: "\e674";
  }
  &.active {
    color: #fbab06;
  }
}

.five_stars {
  height: 40px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
</style>
